﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace FileRenamer
{
    class ConfigFile
    {
        private String fileName;
        private XmlDocument doc= null;        
        public ConfigFile(String name)
        {
            LoadFile(name);
        }

        public ConfigFile()
        {
            String processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
            LoadFile(processName + ".config.xml");
        }


        private void LoadFile(string name)
        {
            doc = new XmlDocument();
            fileName = name;
            if (File.Exists(fileName))
            {
                try
                {                    
                    doc.Load(fileName);                    
                }
                catch (Exception e)
                {
                    Trace.TraceWarning(e.ToString());
                    
                    CreateDefaultDocument();
                }
            }
            else
            {                
                CreateDefaultDocument();             
            }
        }

        private void CreateDefaultDocument()
        {
            doc= new XmlDocument();
            //Create a document type node and  
            //add it to the document.
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
            /*XmlDocumentType doctype;
            doctype = doc.CreateDocumentType("Configuration", null,
                null, "<!ELEMENT Configuration ANY>");
            doc.AppendChild(doctype);*/
            XmlNode root= doc.CreateElement( "Configuration", "http://www.filerenamer.es/ConfigurationFile");

            doc.AppendChild(root);
        }

        public void AddValue(String name, String value)
        {
            XmlNode root=  doc.DocumentElement;

            XmlElement item= doc.CreateElement(name);
            root.AppendChild(item);            
            XmlAttribute attVal= doc.CreateAttribute("value");
            attVal.Value = value;
            
            item.Attributes.Append(attVal);
        }

        public String GetValue(String name)
        {
            try
            {
                XmlNode node= doc.SelectSingleNode("//name");
                return node.Attributes["value"].Value;
            }
            catch (Exception e)
            {
                Trace.TraceWarning(e.ToString());
                return "";
            }
        }

        public String[] GetAllValues(String name)
        {
            try
            {
                XmlNodeList nodes = doc.SelectNodes("//" + name);
                if (nodes.Count==0)
                    return null;
                String []values= new string[nodes.Count];
                int i = 0;
                foreach (XmlNode node in nodes)
                {
                    values[i++]=  node.Attributes["value"].Value;                    
                }
                return values;
                
            }
            catch (Exception e)
            {
                Trace.TraceWarning(e.ToString());
                return null;
            }
        }


        public void Save()
        {            
            doc.Save(fileName);
        }
        public void Clear()
        {
            doc.DocumentElement.RemoveAll();
        }
        public void ClearElements(String name)
        {
            XmlNodeList list=  doc.DocumentElement.SelectNodes(name);
            foreach (XmlNode node in list)
            {
                doc.DocumentElement.RemoveChild(node);
            }
            
        }
    }
}
